package com.mydatastore.mysql.client;

import com.mydatastore.ClientDataStore;
import com.mydatastore.DataStoreException;
import com.mydatastore.MyDataStore;
import com.mydatastore.StatementUnit;
import com.mydatastore.entity.Client;
import com.mydatastore.entity.Sex;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClientDataStoreMySQL implements ClientDataStore {

    private Connection connection;

    public ClientDataStoreMySQL(Connection connection) {
        this.connection = connection;
    }

    @Override
    public int addClient(Client client) throws DataStoreException {
        try {
            StatementUnit su = new AddClient(client);
            PreparedStatement st = su.prepare(connection);
            st.executeUpdate();
            ResultSet rs = st.getGeneratedKeys();
            MyDataStore.close(rs, st, connection);
            if (rs.next()) {
                return rs.getInt(1);
            }
            return -1;

        } catch (SQLException ex) {
            throw new DataStoreException(ex.getMessage());
        }

    }

    @Override
    public Client getClientById(int id) throws DataStoreException {
        try {
            Client client = null;
            StatementUnit su = new GetClientById(id);
            PreparedStatement st = su.prepare(connection);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                String firstName = rs.getString("client_fn");
                String middleName = rs.getString("client_mn");
                String lastName = rs.getString("client_ln");
                int age = rs.getInt("client_age");
                Sex sex = Sex.valueOf(rs.getString("client_sex").toUpperCase());
                client = new Client(id, firstName, middleName, lastName, age, sex);
            }
            MyDataStore.close(rs, st, connection);
            return client;
        } catch (SQLException ex) {
            throw new DataStoreException(ex.getMessage());
        }
    }

    @Override
    public List<Client> list() throws DataStoreException {
        try {
            List<Client> l = new ArrayList<>();
            StatementUnit su = new ListClients();
            PreparedStatement st = su.prepare(connection);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("client_id");
                String firstName = rs.getString("client_fn");
                String middleName = rs.getString("client_mn");
                String lastName = rs.getString("client_ln");
                int age = rs.getInt("client_age");
                Sex sex = Sex.valueOf(rs.getString("client_sex").toUpperCase());
                Client client = new Client(id, firstName, middleName, lastName, age, sex);
                l.add(client);
            }
            MyDataStore.close(rs, st, connection);
            return l;
        } catch (SQLException ex) {
            throw new DataStoreException(ex.getMessage());
        }
    }
}
